package com.clementheliou.cheliou.dal.model.mark;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Type;
import org.joda.time.LocalDateTime;

import com.clementheliou.cheliou.dal.model.base.BaseEntity;
import com.clementheliou.cheliou.dal.model.member.Member;
import com.clementheliou.cheliou.dal.model.photo.Photo;

/**
 * Entity dedicated to the mark data.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link BaseEntity};
 * @see {@link Entity};
 * @see {@link Serializable};
 * @see {@link Table};
 * @see {@link UniqueConstraint}.
 * @since 1.0
 */
@Entity
@Table(name = "mark", uniqueConstraints = @UniqueConstraint(columnNames = { "memberid", "photoid" }))
public class Mark extends BaseEntity implements Serializable {

	/**
	 * UID for serialization.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private static final long serialVersionUID = -5881570395951089652L;

	/**
	 * The mark's addition date.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private LocalDateTime additionDate;

	/**
	 * The mark value in the range from 0 to 5.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Basic(optional = false)
	@Column(nullable = false)
	@Min(0)
	@Max(5)
	@NotNull
	private Integer rank;

	/**
	 * The mark's author.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Member}.
	 * @since 1.0
	 */
	private Member author;

	/**
	 * The photo the mark is related to.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Photo}.
	 * @since 1.0
	 */

	private Photo markedPhoto;

	/*
	 * Getters & setters.
	 */

	@GeneratedValue(generator = "seq_mark")
	@Id
	@SequenceGenerator(name = "seq_mark", sequenceName = "seq_mark")
	public Integer getId() {
		return id;
	}

	@Basic(optional = false)
	@Column(name = "date", nullable = false)
	@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
	@NotNull
	public LocalDateTime getAdditionDate() {
		return additionDate;
	}

	@Transient
	public Date getAdditionJdkDate() {
		return additionDate.toDate();
	}

	public void setAdditionDate(LocalDateTime additionDate) {
		this.additionDate = additionDate;
	}

	public Integer getRank() {
		return rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

	@JoinColumn(name = "memberid", nullable = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@NotNull
	public Member getAuthor() {
		return author;
	}

	public void setAuthor(Member author) {
		this.author = author;
	}

	@JoinColumn(name = "photoid", nullable = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@NotNull
	public Photo getMarkedPhoto() {
		return markedPhoto;
	}

	public void setMarkedPhoto(Photo markedPhoto) {
		this.markedPhoto = markedPhoto;
	}
}
